package com.user.service.impl;

import com.user.dao.IUserdao;
import com.user.dao.impl.UserdaoImpl;
import com.user.entity.BookInfo;
import com.user.entity.Users;
import com.user.service.IUserService;

import java.util.List;

public class UserServiceImpl implements IUserService {

    IUserdao userdao = null;

    public UserServiceImpl() {
        userdao = new UserdaoImpl();
    }

    //用户登录
    @Override
    public boolean userLogin(Users users) {
        return userdao.userLogin(users);
    }

    //用户注册
    @Override
    public boolean userRegister(Users users) {
        return userdao.userRegister(users);
    }

    //用户名是否存在
    @Override
    public boolean queryUsernameIsExist(Users users) {
        return userdao.queryUsernameIsExist(users);
    }

    @Override
    public boolean querySchoolidIsExist(Users users) {

        return userdao.querySchoolidIsExist(users);
    }


    //所有书籍的列表
    @Override
    public List<BookInfo> bookList() {
        List<BookInfo> bookList = userdao.bookList();
        return bookList;
    }

    @Override
    public List<BookInfo> queryBookInfoByPage(int currentPage, int pageSize) {
        return userdao.queryBookInfoByPage(currentPage, pageSize);
    }

    @Override
    public int totalBookInfoCount() {
        return userdao.getBookInfoTotalCount();
    }

    @Override
    public int getBorrowBooksCount(String username) {
        return userdao.getBorrowBooksTotalCount(username);
    }

    @Override
    //根据条件查询书籍
    public List<BookInfo> queryBooksByCondition(BookInfo bookInfo) {
        return userdao.queryBooksByCondition(bookInfo);
    }


    @Override
    public List<BookInfo> queryUserBorrowBooks(Users users) {
        return userdao.usersBooks(users);
    }

    @Override
    //用户还书
    public boolean userReturnBooks(Users users, BookInfo bookInfo) {
        boolean a = userdao.returnBooks(users, bookInfo);
        boolean b = userdao.addBook(bookInfo);
        return a && b;
    }

    @Override
    //用户借书
    public boolean userBorrowBooks(Users users, BookInfo bookInfo) {
        boolean a = userdao.borrowBooks(users, bookInfo);
        boolean b = userdao.reduceBook(bookInfo);
        int id = userdao.getIdByUsername(users.getUsername());
        System.out.println(id);

        boolean c = userdao.UserBorrowBooks(id, bookInfo);
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        return a && b && c;
    }

    @Override
    public boolean queryPasswordIsTure(String username, String password) {
        return password.equals(userdao.getPasswordByUsername(username));
    }


    @Override
    public boolean changeUserPassword(Users users) {
        return userdao.changeUserPassword(users);

    }

    @Override
    public int getIdByUsername(String username) {
        return userdao.getIdByUsername(username);
    }
}
